import md5 from "js-md5"
import { getNowImgPoint } from "./mathFun"
export default class roundTag {
    //id
    id = "";
    //颜色
    color = "yellow";
    //线条宽度
    width = 2;
    //显示
    show = true;
    //起点终点
    position = [];
    //绘制时的点位
    drawPosition = [];
    //边界
    border = [];
    //标注中文
    zhcn = "";
    //标注藏文
    zy = "";
    //标题
    title = "";
    //创建时间
    createdT = null;
    //高亮
    highLight = false;
    //绘制标题
    showTitle = false;

    //起点终点，确定一个圆
    constructor(PointL, PointR, position = null, taggingConfig) {
        this.position[0] = PointL;
        this.position[1] = PointR;
        this.color = taggingConfig.color
        this.width = taggingConfig.width
        if (taggingConfig.objid) {
            this.id = taggingConfig.objid
        } else {
            this.id = md5(new Date().getTime().toString())
        }
        this.zhcn = taggingConfig.zhcn ? taggingConfig.zhcn : ""
        this.zhcn = taggingConfig.zy ? taggingConfig.zy : ""
        this.title = taggingConfig.title ? taggingConfig.title : ""
        this.createdT = taggingConfig.createdT ? taggingConfig.createdT : 0
        if (typeof position == "object") {
            let [x1, y1] = getNowImgPoint(PointL.X, PointL.Y, position)
            let [x2, y2] = getNowImgPoint(PointR.X, PointR.Y, position)
            let coc = [(x2 + x1) / 2, (y2 + y1) / 2]
            let R = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)) / 2
            this.drawPosition = [coc[0], coc[1], R]
            this.border = [coc[0] - R, coc[1] - R, coc[0] + R, coc[1] + R]
        }
    }

    //重设起点终点
    resetPoint(x1, y1, x2, y2, position) {
        this.position[0].X = x1
        this.position[0].Y = y1
        this.position[1].X = x2
        this.position[1].Y = y2
        if (typeof position == "object") {
            let [xx1, yy1] = getNowImgPoint(x1, y1, position)
            let [xx2, yy2] = getNowImgPoint(x2, y2, position)
            let coc = [(xx2 + xx1) / 2, (yy2 + yy1) / 2]
            let R = Math.sqrt(Math.pow((xx2 - xx1), 2) + Math.pow((yy2 - yy1), 2)) / 2
            this.drawPosition = [coc[0], coc[1], R]
            this.border = [coc[0] - R, coc[1] - R, coc[0] + R, coc[1] + R]
        }
    }

    //拖拽时自动重设点位信息
    dragAutoReset(position) {
        let x1 = this.position[0].X
        let y1 = this.position[0].Y
        let x2 = this.position[1].X
        let y2 = this.position[1].Y
        if (typeof position == "object") {
            let [xx1, yy1] = getNowImgPoint(x1, y1, position)
            let [xx2, yy2] = getNowImgPoint(x2, y2, position)
            let coc = [(xx2 + xx1) / 2, (yy2 + yy1) / 2]
            let R = Math.sqrt(Math.pow((xx2 - xx1), 2) + Math.pow((yy2 - yy1), 2)) / 2
            this.drawPosition = [coc[0], coc[1], R]
            this.border = [coc[0] - R, coc[1] - R, coc[0] + R, coc[1] + R]
        }
    }

    //计算点是否在标注中
    inTag(point) {
        return Math.sqrt(Math.pow(point.X - this.drawPosition[0], 2) + Math.pow(point.Y - this.drawPosition[1], 2)) <= this.drawPosition[2];
    }

    get positionP() {
        return [this.position[0].X, this.position[0].Y, this.position[1].X, this.position[1].Y]
    }
}